<?php

namespace MLM\Model\Product\Price;


use MLM\Model\Product\Price;

class Storage extends \MLM\Model\Generic\Db\Storage {
    
    public function save(Price $price){

        $insert = $this->_sql->insert()
                ->into('product_price')
                ->values($price->pull());
        
        $result = $this->execute($insert);
        
        return $result;
    }

    public function edit($dataPrice, $where){

        $update = $this->_sql->update()
                ->table('product_price')
                ->set($dataPrice)
                ->where($where);

        $result = $this->execute($update);

        return $result;

    }

    public function del($priceId){

        $stmt = $this->_db->query("delete from product_price where product_price_id = ? ");
        return $stmt->execute(array($priceId)) ;

    }


    public function getProductPriceList($prodId, $searchField){

        $sql = "select count(1) as total
                from product_price where product_id = $prodId ";

        if ($searchField !=""){
            $sql .= " and $searchField";
        }

        $stmt = $this->_db->query($sql);
        $getTotal = $stmt->execute();

        while($getTotal->next()){
                $res = $getTotal->current();
                $rows = $res['total'];
        }

        $data = array($rows);



        $sql = "select *
                from product_price where product_id = $prodId ";

        if ($searchField !=""){
            $sql .= " and $searchField";
        }

        $sql .= " order by start_dat ";

        if ($limit != ""){
            $sql .= " limit $start, $limit";
        }

        $stmt = $this->_db->query($sql);
        $getPrice = $stmt->execute();

        $dataPrice = array();
        while($getPrice->next()){
                $res = $getPrice->current();

                $aksi = "<a href='javascript: void(0)' onclick='del(".$res['product_price_id'].")'>Hapus</a>";

                array_push($dataPrice, array("HargaAsal" => $res['basic_price'],
                                             "HargaJual" => $res['sale_price'],
                                             "HargaAgen" => $res['agent_price'],
                                             "HargaMember" => $res['agent2_price'],
                                             "TglHarga" => $res['start_dat'].' s/d '.$res['end_dat'],
                                             "Aksi" => $aksi));

        }

        array_push($data, $dataPrice);

        return $data;
    }


    public function getInfoProductPrice($isAgent, $searchField, $start, $limit){

        $sql = "select count(1) as total
                from product p, product_price pp
                where pp.product_id = p.product_id
                and pp.start_dat <= now() and IFNULL(end_dat,now())  >= now() ";


        if ($searchField !=""){
            $sql .= " and $searchField ";
        }

        $stmt = $this->_db->query($sql);
        $getTotal = $stmt->execute();

        while($getTotal->next()){
                $res = $getTotal->current();
                $rows = $res['total'];
        }

        $data = array($rows);



        $sql = "select * from
                product p, product_price pp
                where pp.product_id = p.product_id
                and pp.start_dat <= now() and IFNULL(end_dat,now())  >= now() ";

        if ($searchField !=""){
            $sql .= " and $searchField";
        }

        if ($limit != ""){
            $sql .= " limit $start, $limit";
        }



        $stmt = $this->_db->query($sql);
        $getPrice = $stmt->execute();

        $dataPrice = array();
        while($getPrice->next()){
                $res = $getPrice->current();

                //$harga = ($isAgent == "") ? $res['agent2_price'] : $res['agent_price'];

                array_push($dataPrice, array("kodeProduk" => $res["product_code"],
                                             "namaProduk" => $res["product_name"],
                                             "hargaAgen" =>  $res['agent_price'],
                                             "hargaMember" =>  $res['agent2_price'],
                                             "qty" => $res['qty']
                                             ));

        }

        array_push($data, $dataPrice);

        return $data;
    }
    
}
